package com.eastedu.superise.handler.common;

import com.eastedu.easyexcel.reader.ExcelSheetReader;
import com.eastedu.easyexcel.util.ExcelUtil;
import com.eastedu.manager.ApplyUserManager;
import com.eastedu.model.RequestParameter;
import com.eastedu.superise.handler.common.model.ApplyUserExcelModel;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.util.Collections;
import java.util.List;

/**
 * 更新应用者
 *
 * @author zhangzhishuai
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ApplyUserIdHistoricalDataHandler {

    private final List<ApplyUserManager> applyUserManagerList;


    @SneakyThrows
    public void handle(RequestParameter parameter) {
        File file = new File("C:\\Users\\zhangzhishuai\\Desktop\\刷应用者.xlsx");
        FileInputStream fileInputStream = new FileInputStream(file);
        ExcelSheetReader<ApplyUserExcelModel> excelSheetReader = ExcelSheetReader.of(
                0,
                (applyUserExcelModels, isException) -> applyUserExcelModels.forEach(this::doHandle),
                ApplyUserExcelModel.class);
        ExcelUtil.read(fileInputStream, Collections.singletonList(excelSheetReader));
        log.info("----------------- 更新完成-----------------");
    }

    private void doHandle(ApplyUserExcelModel applyUserExcelModel) {
        applyUserManagerList.forEach(applyUserManager -> {
            if (applyUserManager.supports(applyUserExcelModel.getResourceTypeName())) {
                try {
                    applyUserManager.updateApplyUserId(applyUserExcelModel);
                    log.info("【{}】更新成功", applyUserExcelModel.getResourceId());
                } catch (Exception e) {
                    log.error("更新应用者失败", e);
                }
            }
        });
    }
}
